package com.maizhe.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.maizhe.dao.FileMapper;
import com.maizhe.entity.*;
import com.maizhe.entity.vo.UserVo;
import com.maizhe.service.FileService;
import com.maizhe.util.UUIDGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

@Service
public class FileServiceImpl implements FileService {
	
	@Autowired
	private FileMapper fileMapper;

	public int save(MultipartFile file, UserVo userVo,String status) {
		UploadFile f=new UploadFile();
		String fileName=file.getOriginalFilename();
		String type=fileName.substring(fileName.lastIndexOf("."));
		f.setId(UUIDGenerator.getUUID());
		f.setFileName(fileName);
		f.setFileSize(file.getSize());
		f.setFileType(type);
		f.setFileState(status);
		f.setUploadTime(new Date());
		f.setUploadUser(userVo.getLoginName());
		int i=fileMapper.save(f);
		return i;
	}

	public PageInfo<UploadFile> findAll(UploadFile uploadFile) {
		PageHelper.startPage(uploadFile.getPage(),uploadFile.getRows());
		List<UploadFile> list=fileMapper.findAll(uploadFile);
		return new PageInfo<UploadFile>(list);
	}

	public UploadFile findById(String id) {
		return fileMapper.findById(id);
	}

	public int delete(String id) {
		return fileMapper.delete(id);
	}


}
